package com.toprs.business.controller

import com.alibaba.fastjson.JSONObject
import com.toprs.base.config.RequestHolder
import com.toprs.base.result.PageResult
import com.toprs.base.result.Result
import com.toprs.business.entity.ApplicationBase
import com.toprs.business.query.AppQuery
import com.toprs.business.service.ApplicationService
import com.toprs.business.vo.AppApproveInfo
import com.toprs.business.vo.AppBackInfo
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.servlet.http.HttpServletResponse
import jakarta.validation.Valid
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile


@RestController
@RequestMapping("/application")
@Tag(name = "申请模块", description = "申请模块")
class ApplicationController(val appService: ApplicationService) {


    @PostMapping("/build")
    @Operation(summary = "提交申请")
    fun buildApplication(@Valid @RequestBody application: ApplicationBase): Result<Unit> {
        appService.buildApplication(application)
        return Result.successMsg("新建成功！")
    }

    @PostMapping("/edit")
    @Operation(summary = "修改申请")
    fun editApplication(@Valid @RequestBody application: ApplicationBase): Result<Unit> {
        appService.editApplication(application)
        return Result.successMsg("修改成功，需要手动提交！")
    }


    @PostMapping("/query")
    @Operation(summary = "查询申请")
    fun queryApplication(@Valid @RequestBody appQuery: AppQuery): Result<PageResult<ApplicationBase>> {
        val userId = RequestHolder.getUserId()
        appQuery.userid = userId
        return Result.success(appService.queryApplication(appQuery))
    }

    @GetMapping("/baseInfo/{id}")
    @Operation(summary = "根据申请id获取申请基础信息")
    fun getBaseInfo(@PathVariable id: Int): Result<ApplicationBase> {
        val info: ApplicationBase = appService.getBaseInfo(id)
        return Result.success(info)
    }

    @PostMapping("/back")
    @Operation(summary = "回退申请")
    fun backApp(@Valid @RequestBody appBackInfo: AppBackInfo): Result<Unit> {
        appService.backApp(appBackInfo)
        return Result.successMsg("回退成功！")
    }

    @PostMapping("/approve")
    @Operation(summary = "批准申请")
    fun approveApp(@Valid @RequestBody appApproveInfo: AppApproveInfo): Result<Unit> {
        appService.approveApp(appApproveInfo)
        return Result.successMsg("提交成功！")
    }


    @GetMapping("/close/{id}")
    @Operation(summary = "关闭申请")
    fun closeApp(@PathVariable id: Int): Result<Unit> {
        appService.closeApp(id)
        return Result.successMsg("关闭成功")
    }

    @GetMapping("/search/{serialNumber}")
    @Operation(summary = "模糊查询流水号")
    fun searchSerialNumber(@PathVariable serialNumber: String): Result<List<JSONObject>> {
        val list = appService.searchSerialNumber(serialNumber)
        return Result.success(list)
    }


    @PostMapping("/export-data")
    @Operation(summary = "导出数据")
    fun exportData(@Valid @RequestBody appQuery: AppQuery, httpServletResponse: HttpServletResponse) {
        appService.exportData(appQuery, httpServletResponse)

//        return Result.successMsg("导出成功!")
    }

    @PostMapping("/import-data")
    @Operation(summary = "导入数据")
    fun importData(file: MultipartFile?): Result<Unit> {
        appService.importData(file)
        return Result.successMsg("导入数据成功!")
    }

    @PostMapping("/export-excel")
    @Operation(summary = "导出Excel")
    fun exportExcel(@Valid @RequestBody appQuery: AppQuery, httpServletResponse: HttpServletResponse): Result<Unit> {
        appService.exportExcel(appQuery, httpServletResponse)
        return Result.successMsg("导出成功!")
    }

    @PostMapping("/export-excel-ids")
    @Operation(summary = "根据传入的id导出Excel")
    fun exportExcelByIds(@Valid @RequestBody ids: List<Int>, httpServletResponse: HttpServletResponse): Result<Unit> {
        appService.exportExcelByIds(ids, httpServletResponse)
        return Result.successMsg("导出成功!")
    }


}